/*
 * @Author: zhangxuanjiu
 * @Date: 2017-08-10 14:38:36
 * @Last Modified by: zhangxuanjiu
 * @Last Modified time: 2017-12-04 10:30:55
 */

var gulp = require('gulp');
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config2 = require('./webpack.config-v2.0.js');
var configDev2 = require('./webpack.config-dev-v2.0.js');
var del = require('del');
var path = require('path');
const serverConfig = require('./serverConfig.js')

// 清空文件夹
gulp.task('clean-v2', function() {
  return del([
    './build-v2/*',
    './build-v2/style/*',
    './build-v2/lib/*',
    './build-v2/img/*'
  ]);
});

// 复制
gulp.task('copy-v2', ['clean-v2'], function() {
  gulp.src(['./src-v2/style/*.css'])
    .pipe(gulp.dest('./build-v2/style/'))
  gulp.src(['./src-v2/lib/*.js'])
    .pipe(gulp.dest('./build-v2/lib/'))
  gulp.src(['./src-v2/img/**/*'])
    .pipe(gulp.dest('./build-v2/img/'))
  return gulp.src(['./src-v2/*.html', './src-v2/agentApply/*.html'])
    .pipe(gulp.dest('./build-v2/'));
});

// 监听html/css文件改动
gulp.task('watch-v2', function() {
  gulp.watch('./src-v2/*.html', function(event) {
    gulp.src(['./src-v2/*.html'])
      .pipe(gulp.dest('./build/'))
  });
  gulp.watch('./src-v2/style/*.css', function(event) {
    gulp.src(['./src-v2/style/*.css'])
      .pipe(gulp.dest('./build-v2/style/'))
  });
});

//  v2-dev
gulp.task('dev-v2', ['copy-v2', 'watch-v2'], function() {
  var compiler = webpack(configDev2);
  var server = new WebpackDevServer(compiler, {
    hot: true,
    // enable HMR on the server

    contentBase: path.resolve(__dirname, 'dev'),
    // match the output path

    publicPath: '/',
    // match the output `publicPath`

    stats: { colors: true },

    proxy: {},

    port: serverConfig.port

  });
  server.listen(serverConfig.port, serverConfig.ip);
});

// 执行代码打包
gulp.task('build-v2', ['copy-v2'], function() {
  webpack(config2).run(function() {
    console.log('build finished!')
  });
});